﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using XoLib.Generic;

namespace XoLib.Manager
{

    /// <summary>
    ///   Interface IRegistrator
    /// </summary>
    public interface IRegistrator
    {
      
    }

    /// <summary>
    ///   interface IGenericType
    /// </summary>
    public interface IGenericType<T>
    {
        /// <summary>
        /// <para>
        /// <param name="dic">Dictionary string, T</param>
        /// <param name="ClassType">Type</param>
        /// </para>
        ///   Register
        /// </summary>
        void InsertIn(Dictionary<string, T> dic, Type ClassType);
    }

    /// <summary>
    ///   GenericRegistrator
    /// </summary>
   public abstract class GenericRegistrator<V, T> : Singleton<V>, IRegistrator where V : new()
    {
        /// <summary>
        ///   Dictionary 
        /// </summary>
        public readonly Dictionary<string, T> DicClass = new Dictionary<string, T>();

        /// <summary>
        /// <para>
        /// <param name="key">string</param>
        /// <param name="store">T</param>
        /// </para>
        ///   TryGetValue
        /// </summary>
        public bool TryGetValue(string key, out T store)
        {
            if (!DicClass.TryGetValue(key, out store))
            {              
                return false;
            }
            return true;
        }


        /// <summary>
        /// <para>
        /// <param name="ClassType">Type</param>
        /// </para>
        ///   Register Class U
        /// </summary>
        public void RegisterClass<U>(Type ClassType) where U : IGenericType<T>
        {
          
            var attributes = ClassType.GetCustomAttributes(typeof(U), false);
            if (attributes.Length == 1)
                ((U)attributes[0]).InsertIn(DicClass, ClassType );
            else
                return;
       
        }

        /// <summary>
        /// <para>
        /// <param name="ClassTypes">Type[]</param>
        /// </para>
        ///   Register Class IClass
        /// </summary>
        public void RegisterClass<U>(Type[] ClassTypes) where U : IGenericType<T>
        {
            foreach (Type pType in ClassTypes)
            {
                RegisterClass<U>(pType);
            }
        }
    }
}
